﻿//-----------------------------------------------------------------------
// <copyright>
// This file is subject to the terms and conditions of the Microsoft Public License (MS-PL).
// See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL for more details.
// </copyright>
//----------------------------------------------------------------------- 
using System.Collections.Generic;
using System.Linq;
using Fulcrum.ControlGenerator;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Fulcrum.UnitTests.ControlGenerator
{
    [TestClass]
    public class ControlGeneratorGenerationUnitTests
    {
        public ControlGeneratorGenerationUnitTests()
        {
            // nothing
        }

        ControlGeneratorGenerationUnitTestDouble generationUnit;

        [TestInitialize]
        public void TestInitialize()
        {
            this.generationUnit = new ControlGeneratorGenerationUnitTestDouble();
        }

        [TestMethod]
        public void TestName()
        {
            Assert.AreEqual("ControlGenerator", this.generationUnit.Name);
        }

        [TestMethod]
        public void TestSettings()
        {
            Assert.IsNotNull(this.generationUnit.Settings);
            Assert.IsInstanceOfType(this.generationUnit.Settings, typeof(ControlGeneratorSettings));
        }

        [TestMethod]
        public void TestDataContextsUnloaded()
        {
            Assert.IsNull(this.generationUnit.DataContexts);
        }

        [TestMethod]
        public void TestAllDataContexts()
        {
            var config = CreateGeneratorConfig();

            this.generationUnit.Configuration = config;

            this.generationUnit.CreateDataContexts();

            Assert.AreEqual(2, this.generationUnit.DataContexts.Count);
            Assert.AreEqual("Control1", this.generationUnit.DataContexts.ElementAt(0).Name);
            Assert.AreEqual("Control2", this.generationUnit.DataContexts.ElementAt(1).Name);
        }

        private static MockControlGeneratorConfiguration CreateGeneratorConfig()
        {
            var config = MockConfigurationHelp.CreateGeneratorConfig();

            var contexts = new List<IControlConfiguration>();
            contexts.Add(MockConfigurationHelp.CreateControlConfig("Control1"));
            contexts.Add(MockConfigurationHelp.CreateControlConfig("Control2"));
            config.ControlConfigurations = contexts;
            return config;
        }

        [TestMethod]
        public void TestSpecificDataContext()
        {
            var config = CreateGeneratorConfig();

            this.generationUnit.Configuration = config;

            var settings = (ControlGeneratorSettings)this.generationUnit.Settings;
            settings.Control = "Control2";
            config.SelectedControlConfiguration = config.ControlConfigurations.ElementAt(1);

            this.generationUnit.CreateDataContexts();

            Assert.AreEqual(1, this.generationUnit.DataContexts.Count);
            Assert.AreEqual("Control2", this.generationUnit.DataContexts.ElementAt(0).Name);
        }

    }
}
